package com.hr.mes.controller;

import com.hr.mes.dto.Page;
import com.hr.mes.dto.Result;
import com.hr.mes.enums.MyExceptionEnum;
import com.hr.mes.exception.MyException;
import com.hr.mes.service.ProcessService;
import com.hr.mes.vo.TaskExecuteHisVO;
import com.hr.mes.vo.TaskVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "流程管理")
@CrossOrigin
@RestController
@RequestMapping("/api/process")
public class ProcessController {

    @Autowired
    private ProcessService processService;

    @ApiOperation("部署流程")
    @GetMapping("deployment")
    public Result deployment(@RequestParam("BPMN文件中流程的id") String bpmnKey){
        try{
            String deploymentId = processService.deployment(bpmnKey);
            return Result.ok(deploymentId);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PROCESS_DEPLOY_ERROR, e);
        }
    }

    @ApiOperation("删除流程定义")
    @GetMapping("deleteProcessDefinition")
    public Result deleteProcessDefinition(@ApiParam("流程定义key，也就是BPMN文件中定义的流程id(默认)") @RequestParam String bpmnKey){
        try{
            processService.deleteProcessDefinition(bpmnKey);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PROCESS_DELETE_ERROR, e);
        }
    }

    @ApiOperation("获取用户对应的待办事项")
    @GetMapping("getTaskList")
    public Result getTaskList(){
        try{
            List<com.hr.mes.dto.Task> taskDTOList = processService.getTaskList();
            return Result.ok(taskDTOList);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PROCESS_VIEW_ERROR, e);
        }
    }

    @ApiOperation("获取用户对应的待办事项")
    @GetMapping("getTaskPage")
    public Result getTaskPage(Integer pageNo, Integer pageSize){
        try{
            Page<TaskVO> page = processService.getTaskPage(pageNo, pageSize);
            return Result.ok(page);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PROCESS_VIEW_ERROR, e);
        }
    }

    @ApiOperation("完成节点任务")
    @GetMapping("completeTask")
    public Result completeTask(@ApiParam("表单id") @RequestParam Integer formId,
                               @ApiParam("任务act_ru_task的ID_") @RequestParam String taskId,
                               @ApiParam("下一个审批人") @RequestParam(required = false) String nextAssignee){
        try{
            processService.completeTask(formId, taskId, nextAssignee);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PROCESS_COMPLETE_ERROR, e);
        }
    }

    @ApiOperation("查询流程执行历史")
    @GetMapping("getExecuteHistory")
    public Result getExecuteHistory(@RequestParam String taskId){
        try{
            List<TaskExecuteHisVO> executeHistory = processService.getExecuteHistory(taskId);
            return Result.ok(executeHistory);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PROCESS_VIEW_HIS_ERROR, e);
        }
    }

    @ApiOperation("驳回节点任务")
    @GetMapping("rollbackTask")
    public Result rollbackTask(@ApiParam("目标任务的id") @RequestParam String targetTaskId,
                               @ApiParam("驳回给谁处理") @RequestParam String employeeNumber){
        try{
            com.hr.mes.dto.Task taskDTO = processService.rollbackTask(targetTaskId, employeeNumber);
            return Result.ok(taskDTO);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PROCESS_ROLLBACK_ERROR, e);
        }
    }
}
